var t = require("../../../utils/function.js"), e = getApp(), o = require("../../../api.js");

Page({
    data: {
        formData: [],
        showTime: !1,
        isWait: !1,
        total_price:'',
        phone:'',
        travel_type:'',
        img: t.driverImg,
        driver: {},
        order: {},
        markers: [],
        polyline: [ {
            points: [ {
                longitude: wx.getStorageSync("startAddress").longitude,
                latitude: wx.getStorageSync("startAddress").latitude
            }, {
                longitude: 108.94,
                latitude: wx.getStorageSync("startAddress").latitude
            } ],
            color: "#0299de",
            width: 9,
            dottedLine: !1,
            arrowLine: !0
        } ],
        controls: [ {
            id: 1,
            iconPath: "../../../images/user/icon-22.png",
            position: {
                left: 10,
                top: 10,
                width: 50,
                height: 50
            },
            clickable: !0
        } ],
        user_address: [ {
            lat: null,
            lng: null
        } ],
        longitude: null,
        latitude: null,
        ordernum: 0,
        tiqiankaishi: "0"
    },
    arriveGoal: function(t) {
        var a = this, n = a.data.order_no;
        wx.showModal({
            title: "提示",
            content: "确认您已到达客户身边？",
            success: function(t) {

              
                t.confirm ? wx.getLocation({
                    type: "gcj02",
                    success: function(t) {
                        e.request({
                            url: o.user.driverToStart,
                            method: "post",
                            data: {
                                order_no: n,
                                lat: t.latitude,
                                lng: t.longitude
                            },
                            success: function(t) {
                                e.musicPlay("https://driving.4hl.cn/WeChat/resource/video/pre_start.mp3"), 1 == t.code && (a.getorder(), 
                                e.waitTime(), a.waitTimeCount(), e.endReportHeart());
                            }
                        });

                    }
                }) : (e.endWaitTime(), clearInterval(a.timer));
            }
        });
    },
    waitTimeCount: function() {
        var t = this, e = wx.getStorageSync("timeWait") || 1;
        this.timer = setInterval(function() {
            t.formatSeconds(e++);
        }, 1e3);
    },
    onHide: function() {
        console.log("清除定时器"), clearInterval(this.timer);
    },
    formatSeconds: function(t) {
        var e = parseInt(t), o = 0, a = 0;
        e > 60 && (o = parseInt(e / 60), e = parseInt(e % 60), o > 60 && (a = parseInt(o / 60), 
        o = parseInt(o % 60)));
        var n = parseInt(e) + "秒";
        o > 0 && (n = parseInt(o) + "分" + n), a > 0 && (n = parseInt(a) + "小时" + n), this.setData({
            time: n
        });
    },
    get_lat_lng: function(t) {
        var e = this;
        wx.getLocation({
            type: "gcj02",
            success: function(o) {
                console.log(o, "22222222222222");
                var a = o.latitude, n = o.longitude, i = a + "," + n;
                wx.request({
                    url: "https://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1",
                    data: {
                        key: wx.getStorageSync("MAP_KEY"),
                        location: i
                    },
                    method: "GET",
                    success: function(o) {
                        console.log(o), 
                        e.setData({
                            now_location: o.data.result
                        }), t({
                            now_location: o.data.result.address,
                            latitude: a,
                            longitude: n
                        });
                    }
                });
            }
        });
    },
    openUserLocationBackground: function(t) {
        var e = this;
        wx.getSetting({
            success: function(o) {
                o.authSetting["scope.userLocationBackground"] ? e.startStep(t) : wx.showModal({
                    title: "提示",
                    content: "此功能需要后台运行,确定打开后台运行权限?",
                    success: function(o) {
                        return !!o.confirm && (wx.openSetting({}), e.startStep(t), !0);
                    }
                });
            }
        });
    },
    startStep: function(t) {
        var a = this;
        wx.getLocation({
            type: "gcj02",
            success: function(t) {
                console.log(t, "dsadaslkdhjl;asknd;lkasmnd;lkasmd;lkasmd;lkasnd;lknas");
                var o = t.latitude, a = t.longitude;
                e.driver_position[0].latitude = o, e.driver_position[0].longitude = a;
            }
        });
        var n = this, i = (this.data.tiqiankaishi, t.detail.formId);
        n.startTime();
        var r = this.data.order;
        this.get_lat_lng(function(s) {
            var d = s.now_location, c = s.latitude, l = s.longitude;
            "1" != a.data.tiqiankaishi ? e.request({
                url: o.order.ordering,
                method: "post",
                data: {
                    formid: i,
                    start_point: d,
                    latitude: c,
                    longitude: l,
                    order_no: t.currentTarget.dataset.order_no,
                    before_pics: n.data.formData || ""
                },
                success: function(o) {
                    1 == o.code ? (wx.removeStorageSync("startEvidence"), r.driverid != r.uid && e.gateway(t.currentTarget.dataset.order_no, 2), 
                    wx.redirectTo({
                        url: t.currentTarget.dataset.skip
                    })) : wx.showModal({
                        title: "提示",
                        content: o.msg,
                        success: function() {
                            wx.navigateBack({
                                delta: 1
                            });
                        }
                    });
                }
            }) : wx.showModal({
                title: "提示",
                content: "该订单未到出发时间,请联系客户确认时间",
                success: function(a) {
                    if (a.confirm) {
                        var r = n.data.order;
                        e.request({
                            url: o.order.ordering,
                            method: "post",
                            data: {
                                start_point: d,
                                latitude: c,
                                longitude: l,
                                formid: i,
                                order_no: t.currentTarget.dataset.order_no,
                                before_pics: n.data.formData || ""
                            },
                            success: function(o) {
                                1 == o.code ? (wx.removeStorageSync("startEvidence"), r.driverid != r.uid && e.gateway(t.currentTarget.dataset.order_no, 2), 
                                wx.redirectTo({
                                    url: t.currentTarget.dataset.skip
                                })) : wx.showModal({
                                    title: "提示",
                                    content: o.msg,
                                    success: function() {
                                        wx.navigateBack({
                                            delta: 1
                                        });
                                    }
                                });
                            }
                        });
                    }
                }
            });
        });
    },
    startTime: function() {
        var t = this;
        wx.startLocationUpdateBackground({
            success: function(o) {
                var a = 0;
                wx.onLocationChange(function(o) {
                    ++a % 5 == 0 && e.sendDriverPosition(t.data.order_no);
                });
            }
        });
    },
    intoPageHandler: function(t) {
        var o = this;
      if(this.data.travel_type==1) {
           if (wx.getStorageSync("OPEN_UPLOAD_PIC") && 0 == o.data.formData.length) return wx.navigateTo({
            url: "/pages/driver/evidence/index?type=startEvidence"
        }), !1;
      } 
       
        wx.showModal({
            title: "提示",
            content: "确认乘客已接到并开始行程？", 
            success: function(a) {
                if (!a.confirm) return !1;
                e.endWaitTime(), o.openUserLocationBackground(t), e.musicPlay("https://driving.4hl.cn/WeChat/resource/video/start.mp3"); //您的代驾已开始 请安全驾驶
            }
        });
    },
    callPhoneHandler: function(e) {
        t.callPhoneHandler(e.currentTarget.dataset.phone);
    },
    onLoad: function(t) {
        console.log(t.orderstart, "到达起点状态"), t.orderstart || this.setData({
            showTime: !0
        }), this.setData({
            order_no: t.order_no
        }), t.uid && e.shareCouponsMessage(t.uid);
    },
    getPolyline: function(t) {
        var e = wx.getStorageSync("MAP_KEY"), o = this, a = this.data.polyline, n = t[1].latitude + "," + t[1].longitude + "&to=" + t[0].latitude + "," + t[0].longitude ,p = '&policy=LEAST_DISTANCE';
        wx.request({
            url: "https://apis.map.qq.com/ws/direction/v1/driving/?from=" + n + "&key=" + e + p,
            header: {
                "content-type": "application/json"
            },
            success: function(t) {
                if (console.log(t), 384 === t.data.status) ; else {
                    for (var e = t.data.result.routes[0].polyline, n = 2; n < e.length; n++) e[n] = e[n - 2] + e[n] / 1e6;
                    for (var i = [], r = 0; r < e.length; r += 2) i[i.length] = {
                        longitude: e[r + 1],
                        latitude: e[r]
                    };
                    a[0].points = i, o.setData({
                        polyline: a
                    });
                }
            },
            fail: function() {}
        });
    },
    onReady: function() {},
    onShow: function() {
        this.getorder(), this.data.formData = wx.getStorageSync("startEvidence") || [], 
        wx.getStorageSync("timeWait") && this.waitTimeCount();
    },
    getorder: function() {
        var t = this;
        e.request({
            url: o.order.getfind,
            method: "post",
            data: {
                order_no: t.data.order_no
            },
            success: function(e) {

                wx.setStorageSync('oid', e.data.id)
                console.log(e.data.id);
                wx.hideLoading(), t.setData({
                    order: e.data,
                    markers: e.data.slocaltion,
                    driver: wx.getStorageSync("driver"),
                    longitude: e.data.slocation.location.lng,
                    latitude: e.data.slocation.location.lat,
                    ordernum: e.data.ordernum,
                    elocation: e.data.elocaltion,
                    slocation: e.data.slocation.location,
                    isWait: 4 == e.data.is_service,
                    total_price:e.data.total_price,
                    phone:e.data.phone,
                    travel_type:e.data.travel_type

                }), t.getPolyline(e.data.slocaltion), console.log(t.data.isWait, t.data.showTime, "等待"), 
                wx.setStorageSync("server_phone", e.data.passenger_phone), wx.setStorageSync("ordernum", e.data.ordernum), 
                1 == e.data.is_out && t.setData({
                    tiqiankaishi: "1"
                });
            }
        }), this.setData({}), this.changeMapHeight();
    },
    is_going: function() {
        var t = this;
        e.request({
            url: o.order_branch.setout,
            method: "post",
            data: {
                order_no: t.data.order_no
            },
            success: function(t) {}
        });
    },
    changeMapHeight: function() {
        var t = this;
        wx.getSystemInfo({
            success: function(e) {
                var o = e.windowWidth / 750 * 440, a = e.windowHeight - o, n = [ {
                    id: 1,
                    iconPath: "../../../images/driver/icon-50.png",
                    position: {
                        left: 10,
                        top: a - 56,
                        width: 50,
                        height: 50
                    },
                    clickable: !0
                }, {
                    id: 2,
                    iconPath: "../../../images/driver/dao.png",
                    position: {
                        left: 10,
                        top: a - 110,
                        width: 50,
                        height: 50,
                        
                    },
                    clickable: !0
                }, {
                    id: 3,
                    // iconPath: "../../../images/driver/cancel.png",
                    position: {
                        left: 10,
                        top: a - 164,
                        width: 50,
                        height: 50
                    },
                    clickable: !0
                } ];
                t.setData({
                    winPath: {
                        w: e.windowWidth,
                        h: e.windowHeight
                    },
                    map_height: a,
                    controls: n
                });
            }
        });
    },
    controltap: function(t) {
        1 != t.controlId ? 2 != t.controlId ? 3 != t.controlId ? 4 != t.controlId || this.timelength() : this.cancleOrder() : this.startHandler() : this.getAddress();
    },
    timelength: function() {
        var t = this;
        wx.showModal({
            title: "提示",
            content: "确定开始时长计费？",
            success: function(a) {
                a.confirm && e.request({
                    url: o.order.arrive,
                    method: "post",
                    data: {
                        order_no: t.data.order.order_no
                    },
                    success: function(t) {
                        t.code, wx.showToast({
                            title: t.msg,
                            icon: "none"
                        });
                    }
                });
            }
        });
    },
    cancleOrder: function() {
        var t = this.data.order.order_no;
        wx.navigateTo({
            url: "/pages/driver/deleteOrderMasker/index?order_no=" + t
        });
    },
    getAddress: function() {
        var t = this;
        wx.getLocation({
            success: function(e) {
                t.setData({
                    latitude: e.latitude,
                    longitude: e.longitude
                });
            }
        });
    },
    startHandler: function() {
        var t = this, e = this.data.slocation;
        e.lat && e.lng && wx.openLocation({
            latitude: e.lat,
            longitude: e.lng,
            name: t.data.order.start_adds
        });
    }
});